﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataAccess;
using DevExpress.XtraEditors;
using DevExpress.XtraRichEdit.API.Word;
using DevExpress.XtraTreeList.Nodes;

namespace QuanLyKhachSan
{
    public partial class FormHoaDonDichVuCapNhat : XtraForm
    {
        private readonly HoaDonDichVuManager _hoaDonDichVuManager = new HoaDonDichVuManager();
        private int mahoaDv = -1;

        public FormHoaDonDichVuCapNhat()
        {
            InitializeComponent();
        }

        public FormHoaDonDichVuCapNhat(HoaDonDichVu hoaDonDich)
        {
            InitializeComponent();
            InitHoaDonDichVu(hoaDonDich);
        }

        private void InitHoaDonDichVu(HoaDonDichVu hoaDonDich)
        {
            txtmaHD.Text = hoaDonDich.MaHoaDonDV.ToString();
            txtTenHD.Text = hoaDonDich.TenHoaDon;
            dateEditNgaySD.EditValue = hoaDonDich.NgaySuDung;
            dateEditNgayThanhToan.EditValue = hoaDonDich.NgayThanhToan;
            cbbTrangThai.SelectedItem = hoaDonDich.TrangThai;
            lEditMaKH.EditValue = hoaDonDich.MaKH;
            lEditMaNV.EditValue = hoaDonDich.MaNV;
            mahoaDv = hoaDonDich.MaHoaDonDV;
            dgvDichVu.Enabled = false;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Close();
        }

        public bool ThemChiTietHoaDon(int maHoaDon)
        {
            var ctManager = new ChiTietHoaDonDVManager();
            var ctObject = new ChiTietHoaDonDichVu();
            try
            {
                ctObject.SoLuong = 1;
                ctObject.MaHoaDonDV = maHoaDon;
                foreach (TreeListNode item in dgvDichVu.Nodes)
                {
                    if (item.Checked)
                    {
                        try
                        {
                            ctObject.MaDV = Convert.ToInt32(item[0].ToString());
                            ctManager.Add(ctObject);
                        }
                        catch (Exception ex)
                        {
                            XtraMessageBox.Show(ex.Message);
                        }
                    }
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public List<ChiTietHoaDonDichVu> LayChiTietHoaDon()
        {
            var rs = new List<ChiTietHoaDonDichVu>();
            foreach (TreeListNode item in dgvDichVu.Nodes)
            {
                if (item.Checked)
                {
                    try
                    {
                        var ctObject = new ChiTietHoaDonDichVu();
                        ctObject.MaDV = Convert.ToInt32(item[0].ToString());
                        ctObject.SoLuong = 1;
                        rs.Add(ctObject);
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show(ex.Message);
                    }
                }
            }
            return rs;
        }

     private HoaDonDichVu GetData()
        {
            var hoaDonDichVu = new HoaDonDichVu();
            try
            {
                hoaDonDichVu.TenHoaDon = txtTenHD.Text;
                hoaDonDichVu.NgaySuDung = Convert.ToDateTime(dateEditNgaySD.EditValue);
                hoaDonDichVu.NgayThanhToan = Convert.ToDateTime(dateEditNgayThanhToan.EditValue);
                hoaDonDichVu.TrangThai = cbbTrangThai.EditValue.ToString();
                
                //get money from orderdetails by order
                hoaDonDichVu.ThanhTien = GetMoney();
                hoaDonDichVu.ChiTietHoaDonDichVus = LayChiTietHoaDon();
                hoaDonDichVu.MaNV = Convert.ToInt32(lEditMaNV.EditValue);
                hoaDonDichVu.MaKH = lEditMaKH.EditValue.ToString();
                return hoaDonDichVu;
            }
            catch (Exception ex )
            {
                XtraMessageBox.Show("Thông tin bạn nhập không hợp lệ: " + ex.Message,"Lỗi",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                return null;
            }
        }

        private decimal GetMoney()
        {
            decimal thanhTien = 0;
            var dv = new DichVuManager();
            foreach (TreeListNode item in dgvDichVu.Nodes)
            {
                if (item.Checked)
                {
                    int dvId = int.Parse(item[0].ToString());
                    thanhTien += dv.Find(d => d.MaDV.Equals(dvId)).Result.Single<DichVu>().Gia;
                }
            }
            return thanhTien;
        }

        private void btnThaoTac_Click(object sender, EventArgs e)
        {
            var orderService = GetData();
            if (orderService != null)
            {
                var validateRs = _hoaDonDichVuManager.Validate(orderService);
                if (validateRs.Valid())
                {
                    if (btnThaoTac.Text.CompareTo("Add") == 0)
                    {
                        _hoaDonDichVuManager.Add(orderService);
                        //ThemChiTietHoaDon(orderService.MaHoaDonDV);
                    }
                    else
                    {
                        //update
                        orderService.MaHoaDonDV = Convert.ToInt32(txtmaHD.Text);
                        _hoaDonDichVuManager.InsertOrUpdate(orderService);
                    }

                    DialogResult = DialogResult.OK;
                    Close();
                }
                else
                {
                    XtraMessageBox.Show(validateRs.ToString(), "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void HienThiDichVuHoaDon(int maHoaDon)
        {
            var listChiTiet = new ChiTietHoaDonDVManager().Find(ct => ct.MaHoaDonDV.Equals(maHoaDon)).Result;
            foreach (ChiTietHoaDonDichVu item in listChiTiet)
            {
                foreach (TreeListNode node in dgvDichVu.Nodes)
                {
                    try
                    {
                        int maDv = int.Parse(node[0].ToString());
                        if (item.MaDV.Equals(maDv))
                        {
                            node.Checked = true;}
                    }
                    catch (Exception ex)
                    {
                        XtraMessageBox.Show(ex.Message);
                    }

                }
            }
        }

        private void LoadDanhSachDichVu()
        {
            dgvDichVu.DataSource = new DichVuManager().GetAllServices().Result.Where(dv => dv.TrangThai != false).ToList();
            if (mahoaDv != 1)
            {
                HienThiDichVuHoaDon(mahoaDv);
            }
        }

        private void FormHoaDonDichVuCapNhat_Load(object sender, EventArgs e)
        {
            lEditMaKH.Properties.DataSource = new KhachHangManager().LayDanhSach().Result.ToList();
            lEditMaNV.Properties.DataSource = new NhanVienManager().LayDanhSach().Result.ToList();
            LoadDanhSachDichVu();
        }
    }
}
